<?php


$tmp_file=$_FILES['file_import']['tmp_name'];
//$array = preg_split("/[\r\n]+/", file_get_contents("fileku.txt"));
$array = preg_split("/[\r\n]+/", file_get_contents($tmp_file));
foreach($array as $line){
	$arr_baru[]=explode(";",$line);
}
$arr_gagal[]=$arr_baru[0];
//echo "<pre>";
//print_r($_FILES);
//print_r($arr_baru);
//$i=0;
$now=date('YmdHis');
$file_log=fopen("log_import/log_import_".$now.".txt","w");
fputs($file_log,"LOG IMPORT#Daftar History Import Klaim INA CBG#".date('d-m-Y H:i:s')."\r\n");
fputs($file_log,"=====================================detail import=========\r\n");
$sukses=0;
$gagal=0;
foreach($arr_baru as $key=>$val){
	if($key>0){
		$status_insert=true;
		//buat file log
		
		
		$no_klaim = kode_klaim();
		$id_jaminan=$val[2];
		$tgl_masuk=explode("/",$val[6]);
		$tgl_masuk=$tgl_masuk[2]."-".$tgl_masuk[1]."-".$tgl_masuk[0];
		$tgl_keluar=explode("/",$val[7]);
		$tgl_keluar=$tgl_keluar[2]."-".$tgl_keluar[1]."-".$tgl_keluar[0];
		$tgl_trans=date('Y-m-d');
		$umur=$val[11];
		$id_ppk=$val[0];
		$total_biaya=$val[4];
		$jenis_layanan=$val[12];
		$verifikator='1';
		$sql_klaim="insert into klaim(id_klaim,id_ppk,id_jaminan,umur,tgl_masuk,tgl_keluar,
		tgl_trans,total_diminta,jenis_klaim,id_jenis_layanan,id_verifikator) 
		values('".$no_klaim."','".$id_ppk."','".$id_jaminan."','".$umur."','".$tgl_masuk."','".$tgl_keluar."','".$tgl_trans."','".$total_biaya."','inacbg','".$jenis_layanan."','".$verifikator."')";
		//$sql="insert into coba values('".$val[0]."','".$val[1]."','".$val[2]."');";
		//echo $sql_klaim."<br>";
		$qry_klaim=mysql_query($sql_klaim);
		$status_insert= $status_insert && $qry_klaim;
		
		if($status_insert==true){
			//echo $sql_klaim."<br>";
	
			//sql diagnosis
			for($i=16;$i<=44;$i++){
				if($val[$i]!=""){
					//cek kalau ndak kosong
					$sql_diagnosis="insert into klaim_diagnosis(id_klaim,icd10) values ('".$no_klaim."','".$val[$i]."')";
					$qry_diagnosis=mysql_query($sql_diagnosis);
					//echo $sql_diagnosis."<br>";
					$status_insert= $status_insert && $qry_diagnosis;
				}
			}
	
			//buat prosedur
			for($i=45;$i<=74;$i++){
				if($val[$i]!=""){
					//cek kalau ndak kosong
					$sql_prosedur="insert into klaim_prosedur(id_klaim,icd9cm) values ('".$no_klaim."','".$val[$i]."')";
					$qry_prosedur=mysql_query($sql_prosedur);
					//echo $sql_diagnosis."<br>";
					$status_insert= $status_insert && $qry_prosedur;
				}
			}
	
			//buat inacbg
			$sql_ina="insert into klaim_inacbg(id_klaim,kode_inacbg,tarif_ditanggung) values 
				('".$no_klaim."','".$val[76]."','".$val[77]."')";
			$qry_ina=mysql_query($sql_ina);
					//echo $sql_diagnosis."<br>";
			$status_insert= $status_insert && $qry_ina;
			//echo $sql_ina."<br>";
	
		}
		
		if($status_insert==true){
			//sukses
			fputs($file_log,"Data Pada Baris ke ".$key." berhasil diimport ke sistem.\r\n");
			$sukses++;
		}else{
			//gagal
			fputs($file_log,"Data Pada Baris ke ".$key." gagal diimport ke sistem.\r\n");
			$gagal++;
			//hapus klaim
			hapus_klaim($no_klaim);
			$arr_gagal[]=$arr_baru[$key];
		}
	}
	//$i++;
}
if($gagal>0){
	//buat file txt data yg gagal
	$nama_file_gagal="log_gagal_".$now.".txt";
	$file_error=fopen("log_file_gagal/".$nama_file_gagal,"w");
	foreach($arr_gagal as $val){
		$data_error=implode(";", $val)."\r\n";
		fputs($file_error,$data_error);
	}
}
$total=$sukses+$gagal;
fputs($file_log,"\r\nRESUME\r\n");
fputs($file_log,"Total  ".$total." data, berhasil diimport ".$sukses." data, gagal diimport ".$gagal." data\r\n");
fputs($file_log,"=====================================last detail import====\r\n");
fclose($file_log);
?>
<div id="tiga_ba">
	<div id="tiga_baa">Status Import Klaim</div>
	
	<div class="clear"></div>
</div>
<h2>Data Sudah Selesai Diimport</h2>
<p>Klik <a target="_blank" href="<?php echo "log_import/log_import_".$now.".txt"; ?>" >Disini</a>, untuk melihat log import</p>
<?php
if($gagal>0){
?>
<p>Klik <a target="_blank" href="<?php echo "log_file_gagal/".$nama_file_gagal; ?>" >Disini</a>, untuk melihat mendownload data klaim yang gagal</p>
<?php
}
?>
<br>
<br>
<p><a href="utama.php?module=mod_klaim_ina">Kembali</a></p>